package com.devnn.view;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.devnn.base.R;


public class ConfirmDialog extends Dialog implements View.OnClickListener {
    private Button btnOK, btnCancel;
    private TextView tvMessage;
    private Context context;
    private OnSelectedListener onSelectedListener;

    public interface OnSelectedListener {
        void onSureSelected();
        void onCancelSelected();
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public ConfirmDialog(Context context) {
        super(context);
        this.context = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_dialog_confirm);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        this.getWindow().setLayout(context.getResources().getDisplayMetrics().widthPixels - 40, LayoutParams.WRAP_CONTENT);
        initView();
        setEvent();
    }

    public void setCancelVisiable(int flag) {
        this.btnCancel.setVisibility(flag);
    }

    private void initView() {
        tvMessage = (TextView) findViewById(R.id.confirm_message);
        btnOK = (Button) findViewById(R.id.confirm_ok);
        btnCancel = (Button) findViewById(R.id.confirm_cancel);

    }

    private void setEvent() {
        btnOK.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    public void setMessage(String msg) {
        tvMessage.setText(msg);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.equals(this.btnCancel)) {
            if (this.onSelectedListener != null) {
                this.onSelectedListener.onCancelSelected();
            }

        } else if (v.equals(this.btnOK)) {
            if (this.onSelectedListener != null) {
                this.onSelectedListener.onSureSelected();
            }
        }
        this.dismiss();
    }
}
